namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class SubmissionControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SubmissionController>()
                .DerivesFrom<Controller>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IRegisterRoutes>()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SubmissionController());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new SubmissionController() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new SubmissionController() as IRegisterRoutes).Register(routes);

            Assert.IsType<SubmissionRoute>(routes[0]);
        }

        [Fact]
        public void op_Process()
        {
            FakeRepository.Clear();
            SubmissionConfiguration.Clear();

            try
            {
                Token token = Token.New();

                FakeRepository.Register<FormSubmission>(new
                {
                    token = token,
                    urn = "urn://example.com/submission/" + Token.New(),
                    xml = 
                        "<submission>" +
                            "<data>" +
                                "<value name='handler'>test</value>" +
                            "</data>" +
                        "</submission>"
                });

                ServiceLocator.Register<IProcessSubmission, TestIProcessSubmission>("component");

                SubmissionConfiguration.Settings().Add((
                    "<handler name='test'>" +
                        "<processor id='component' />" +
                    "</handler>").Deserialize<SubmissionHandlerSetting>());

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/test?" + token).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                SubmissionController controller = new SubmissionController();
                controller.ControllerContext = new ControllerContext(mock.Object, new RouteData(), controller);

                ActionResult result = controller.Process();

                Assert.IsAssignableFrom<SeeOtherResult>(result);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                SubmissionConfiguration.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}